package lagou.homework.lagou_homework_rocketmq.controller;

import lagou.homework.lagou_homework_rocketmq.common.ResponseResult;
import lagou.homework.lagou_homework_rocketmq.entity.Order;
import lagou.homework.lagou_homework_rocketmq.exception.SeckillException;
import lagou.homework.lagou_homework_rocketmq.repository.OrderRepository;
import lagou.homework.lagou_homework_rocketmq.service.OrderService;
import lagou.homework.lagou_homework_rocketmq.utils.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.SortParameters;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Slf4j
@Controller
@RequestMapping("order")
public class OrderController {
    @Autowired
    private OrderRepository orderRepository;
    @Autowired
    private OrderService orderService;

    @RequestMapping("/toPay")
    public ModelAndView toPay(String orderNo,String seckillId){
        /**
         * 查找订单
         */
        Order order = orderRepository.findByOrderNo(orderNo);
        if(order == null){
            throw new SeckillException("500","没有查询到订单",null);
        }
        ModelAndView mv = new ModelAndView();
        mv.addObject("order",order);
        mv.addObject("seckillId",seckillId);
        mv.setViewName("pay");
        return mv;
    }

    @RequestMapping("/pay")
    public ModelAndView pay(String orderNo,String sekillId){
        ModelAndView mv = new ModelAndView();
        String result = null;
        try {
           result = orderService.payOrder(orderNo,sekillId);
        } catch (Exception e) {
            log.error("支付失败",e);
            result = "支付失败";
        }
        mv.addObject("result",result);
        mv.addObject("orderNo",orderNo);
        mv.setViewName("pay_result");
        return mv;
    }
}
